<?php


namespace app\crontab\controller\once;


use custom\Log;
use custom\Mongo;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Exception;

/**
 * 来源省市区
 * Class SourceLocation
 * @package app\crontab\controller
 */
class SourceLocation extends Command {


    protected function configure() {
        $this->setName('SourceLocation');
    }

    protected function execute(Input $input, Output $output) {
        try {
            echo "start\n";
            $mongo = Mongo::getInstance();
            $ali_house = db("house")
                ->field('title,id')
                ->where('id', '>', 8430)
                ->where('source_type', 1)
                ->order('id', 'asc')
                ->select();
            $options = [
                'sort'  => ['_id' => -1],//逆排序
                'limit' => 1,
            ];
            $write = [];
            foreach ($ali_house as $k1 => $v1) {
                $cursor = $mongo->select('house_jd', ['title' => $v1['title']], $options);
                $arr = (array)$cursor[0];
                if (empty($arr)) {
                    $write[] = $v1['id'];
                } else {
                    $province = $arr['province'] ?? '';
                    $city = $arr['city'] ?? '';
                    $area = $arr['area'] ?? '';
                    db("house")
                        ->where('id', $v1['id'])
                        ->update(['source_province' => $province, 'source_city' => $city, 'source_area' => $area]);
                }
            }
            Log::write($write, "SourceLocation.log");
            echo "end\n";
        } catch (Exception $e) {
            Db::close();
            $err = "异常信息:{$e->getMessage()} | 所在的代码行:{$e->getLine()} | 文件{$e->getFile()} | php 版本 " . PHP_VERSION . "(" . PHP_OS . ")";
            Log::write($err, "UserAutoPush_Err.log");
        }
    }

}
